package com.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.Employee;
import com.service.EmployeeService;
import com.service.ProjectService;

public class EditProject extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		request.getSession().setAttribute("id", id);
		
		ProjectService service=new ProjectService();
		com.entity.Project project= service.getProject(id);
		
		EmployeeService employeeService=new EmployeeService();
		List<Employee> employees=employeeService.getAll();
		Employee admin=employeeService.getAdmin(id);
		List<Employee> workers=employeeService.getWorkers(id);
		
		request.setAttribute("employees", employees);
		request.setAttribute("admin", admin);
		request.setAttribute("workers", toS(workers));
		request.setAttribute("project", project);
		
		request.getRequestDispatcher("WEB-INF/views/editProject.jsp").forward(request, response);
	}
	
	private String toS(List<Employee> workers){
		String s="";
		for (int i = 0; i < workers.size(); i++) {
			if (i==workers.size()-1) {
				s+=workers.get(i).getUsername();
			}else {
				s+=(workers.get(i).getUsername()+",");
			}
		}
		return s;
	}

}
